package com.conversationboard.view;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;

import com.conversationboard.formatter.DateFormatter;
import com.conversationboard.model.PendingRegistration;
import com.conversationboard.model.PendingRegistrations;
import com.conversationboard.model.User;
import com.conversationboard.network.UserLocation;

public class PendingRegistrationsDisplay {

	public static String display(PendingRegistrations pendingRegistrations, User user) throws SQLException, UnsupportedEncodingException {

		StringBuilder buffer = new StringBuilder();

		buffer.append("<table class='table'>");

		buffer.append("<tr class='row'>");
		buffer.append("<td class='header'><strong>Login ID</strong></td>");
		buffer.append("<td class='header'><strong>Display Name</strong></td>");
		buffer.append("<td class='header'><strong>Registration Time</strong></td>");
		buffer.append("<td class='header'><strong>IP Address</strong></td>");
		buffer.append("<td class='header'><strong>Domain Name</strong></td>");
		buffer.append("<td class='header'><strong>Selected Time Zone</strong></td>");
		buffer.append("<td class='header'><strong>Location (from IP)</strong></td>");
		buffer.append("<td class='header'><strong>Accept</strong></td>");
		buffer.append("<td class='header'><strong>Reject</strong></td>");
		buffer.append("</tr>");

		/* Display all of the pending registrations */

		for (PendingRegistration pendingRegistration : pendingRegistrations) {

			buffer.append("<tr class='row'>");

			String time = DateFormatter.prettyPrintTime(pendingRegistration.getRegistrationTime(), user, false, true);

			buffer.append("<td class='cell'>" + pendingRegistration.getLoginId() + "</td>\n");
			buffer.append("<td class='cell'>" + pendingRegistration.getDisplayName() + "</td>\n");
			buffer.append("<td class='cell'>" + time + "</td>\n");
			buffer.append("<td class='cell'>" + pendingRegistration.getIpAddress() + "</td>\n");
			buffer.append("<td class='cell'>" + pendingRegistration.getDomainName() + "</td>\n");
			buffer.append("<td class='cell'>" + pendingRegistration.getTimeZone() + "</td>\n");
			buffer.append("<td class='cell'>" + UserLocation.getLocation(pendingRegistration.getIpAddress()) + "</td>\n");
			buffer.append("<td class='cell'><input type='radio' name='" + pendingRegistration.getLoginId() + "' value='accept'></input></td>\n");
			buffer.append("<td class='cell'><input type='radio' name='" + pendingRegistration.getLoginId() + "' value='reject'></input></td>\n");

			buffer.append("</tr>\n");

		}

		buffer.append("</table>");

		return buffer.toString();

	}
}
